﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmTdTc : Form
    {
        public FrmTdTc()
        {
            InitializeComponent();
        }

        private void FrmTdTc_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab == tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.ds_tdtc_1;
            }
            else
            {
                if (tabControl2.SelectedTab == tabControl2.TabPages[1])
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.ds_tdtc_2;
                }
                else
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.ds_tdtc_3;
                }
               
            }
        }

        private void FrmTdTc_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(567, 570);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrTdTc prTdTc = new PrTdTc();
            prTdTc.Show();
            this.Hide();
        }

        private void FrmTdTc_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }

         // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong dau
            Values = txtTDau_DayMTr.Text;//0
            Values = Values +"\t"+ txtTDau_DayMSau.Text;//1
            Values = Values +"\t"+ txtTDau_Day.Text;//2
            Values = Values +"\t"+ txtTDau_doc.Text;//3
            Values = Values +"\t"+ txtTDau_mrongT.Text;//4
            Values = Values +"\t"+ txtTDau_mrongS.Text;//5
            //Tuong canh
            Values = Values +"\t"+ TxtTcCaoH2.Text;//6
            Values = Values +"\t"+ TxtTcDaySan.Text;//7
            Values = Values +"\t"+ TxtTcSauCk.Text;//8
            Values = Values +"\t"+ txtTcMrongtuong.Text;//9
            Values = Values +"\t"+ TxtTcDayCk.Text;//10
            Values = Values +"\t"+ TxtTcGoca1.Text;//11
            Values = Values +"\t"+ TxtTcMorongm2.Text;//12
            //Gia co
            Values = Values +"\t"+ TxtDaySanGc.Text;//13
            Values = Values +"\t"+ TxtSauMongGc.Text;//14
            Values = Values +"\t"+ TxtDayMongGc.Text;//15
            Values = Values +"\t"+ txtMorongsan.Text;//16
            Values = Values +"\t"+ txtBotriCk.Checked.ToString();//17
            Values = Values +"\t"+ txtNghiengMong.Text;//18
            Values = Values +"\t"+ txtLopdem.Text;//19
            Values = Values +"\t"+ TxtTcDaytuong.Text;//20
            Values = Values +"\t"+ txtDuoiTc.Text;//21
            Values = Values +"\t"+ txtCaotaluyh1.Text;//22
            Values = Values +"\t"+ txtCaotaluyh2.Text;//23
            //Du lieu bo sung cho phien ban 2011
            //Tuong dau
            Values = Values +"\t"+ txtTDau_cao.Text;//24
            Values = Values +"\t"+ txtTDau_mrongB.Text;//25
            Values = Values +"\t"+ txtTDau_mrongb4.Text;//26
            //03/2011
            Values = Values +"\t"+ txtTDau_Rongmong.Text;//27
            Values = Values +"\t"+ opt_b3.Checked.ToString();//28
            Values = Values +"\t"+ opt_b.Checked.ToString();//29
            //san gc
            Values = Values +"\t"+ TxtDaiSanGc.Text;//30
            Values = Values +"\t"+ opt_sanb.Checked.ToString();//31
            Values = Values +"\t"+ opt_sanb3.Checked.ToString();//32
            regKey.SetValue("KtTdTc", Values);
            regKey.Close();
        }

         // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtTdTc").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                //Tuong dau
                txtTDau_DayMTr.Text=STR[i++];//0
                txtTDau_DayMSau.Text = STR[i++];//1
                txtTDau_Day.Text = STR[i++];//2
                txtTDau_doc.Text = STR[i++];//3
                txtTDau_mrongT.Text = STR[i++];//4
                txtTDau_mrongS.Text = STR[i++];//5
                //Tuong canh
                TxtTcCaoH2.Text = STR[i++];//6
                TxtTcDaySan.Text = STR[i++];//7
                TxtTcSauCk.Text = STR[i++];//8
                txtTcMrongtuong.Text = STR[i++];//9
                TxtTcDayCk.Text = STR[i++];//10
                TxtTcGoca1.Text = STR[i++];//11
                TxtTcMorongm2.Text = STR[i++];//12
                //Gia co
                TxtDaySanGc.Text = STR[i++];//13
                TxtSauMongGc.Text = STR[i++];//14
                TxtDayMongGc.Text = STR[i++];//15
                txtMorongsan.Text = STR[i++];//16
                txtBotriCk.Checked = bool.Parse(STR[i++]);//17
                txtNghiengMong.Text = STR[i++];//18
                txtLopdem.Text = STR[i++];//19
                TxtTcDaytuong.Text = STR[i++];//20
                txtDuoiTc.Text = STR[i++];//21
                txtCaotaluyh1.Text = STR[i++];//22
                txtCaotaluyh2.Text = STR[i++];//23
                //Du lieu bo sung cho phien ban 2011
                //Tuong dau
                txtTDau_cao.Text = STR[i++];//24
                txtTDau_mrongB.Text = STR[i++];//25
                txtTDau_mrongb4.Text = STR[i++];//26
                //03/2011
                txtTDau_Rongmong.Text = STR[i++];//27
                opt_b3.Checked = bool.Parse(STR[i++]);//28
                opt_b.Checked = bool.Parse(STR[i++]);//29
                //san gc
                TxtDaiSanGc.Text = STR[i++];//30
                opt_sanb.Checked = bool.Parse(STR[i++]);//31
                opt_sanb3.Checked = bool.Parse(STR[i++]);//32
                regKey.Close();
            }
        }

         // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.dau)|*.dau";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Tuong dau
                Str = "------TUONG DAU------"  + "\r\n";
                Str = Str + "Cao dinh tuong H1:" +"\t"+ txtTDau_cao .Text + "\r\n";
                Str = Str + "Day mong truoc H2:" +"\t"+ txtTDau_DayMTr .Text + "\r\n";
                Str = Str + "Day mong sau H3:" +"\t"+ txtTDau_DayMSau .Text + "\r\n";
                Str = Str + "Cao dinh taluy H4:" +"\t"+ txtCaotaluyh1 .Text + "\r\n";
                Str = Str + "Cao chan taluy H5:" +"\t"+ txtCaotaluyh2 .Text + "\r\n";
                Str = Str + "Day dinh tuong b1:" +"\t"+ txtTDau_Day .Text + "\r\n";
                Str = Str + "Mo mong truoc b2:" +"\t"+ txtTDau_mrongT .Text + "\r\n";
                Str = Str + "Mo mong sau b3:" +"\t"+ txtTDau_mrongS .Text + "\r\n";
                Str = Str + "Rong mong b:" +"\t"+ txtTDau_Rongmong .Text + "\r\n";
                Str = Str + "Gan theo mo rong:" +"\t"+ opt_b3.Checked.ToString() + "\r\n";
                Str = Str + "Gan theo rong mong:" +"\t"+ opt_b.Checked.ToString() + "\r\n";
                Str = Str + "Mo rong tuong b4:" +"\t"+ txtTDau_mrongb4 .Text + "\r\n";
                Str = Str + "Doc lung tuong i:" +"\t"+ txtTDau_doc .Text + "\r\n";
                //Tuong canh
                Str = Str + "------TUONG CANH------"  + "\r\n";
                Str = Str + "Cao tuong H1:" +"\t"+ TxtTcCaoH1 .Text + "\r\n";
                Str = Str + "Cao tuong H2:" +"\t"+ TxtTcCaoH2 .Text + "\r\n";
                Str = Str + "Sau chan khay H3:" +"\t"+ TxtTcSauCk .Text + "\r\n";
                Str = Str + "Day san cong H4:" +"\t"+ TxtTcDaySan .Text + "\r\n";
                Str = Str + "Mo rong dinh b1:" +"\t"+ txtTcMrongtuong .Text + "\r\n";
                Str = Str + "Rong cuoi san b2:" +"\t"+ txtDuoiTc .Text + "\r\n";
                Str = Str + "Day chan khay b3:" +"\t"+ TxtTcDayCk .Text + "\r\n";
                Str = Str + "Chan khay trong tuong canh:" +"\t"+ txtCkTrongTc .Text + "\r\n";
                Str = Str + "Day dinh tuong b4:" +"\t"+ TxtTcDaytuong .Text + "\r\n";
                Str = Str + "Mo rong mong b5:" +"\t"+ txtTDau_mrongB .Text + "\r\n";
                Str = Str + "Mo rong mong b6:" +"\t"+ TxtTcMorongm2 .Text + "\r\n";
                Str = Str + "Goc mo tren:" +"\t"+ TxtTcGoca1 .Text + "\r\n";
                Str = Str + "Goc mo duoi:" +"\t"+ TxtTcGoca2 .Text + "\r\n";
                Str = Str + "Doc taluy tuong:" +"\t"+ txtTcDocm .Text + "\r\n";
                //Gia co
                Str = Str + "------GIA CO------"  + "\r\n";
                Str = Str + "Bo tri san gia co:" +"\t"+ txtBotriSgc .Text + "\r\n";
                Str = Str + "Day san gia co H1:" +"\t"+ TxtDaySanGc .Text + "\r\n";
                Str = Str + "Dai san gia co b1:" +"\t"+ TxtDaiSanGc .Text + "\r\n";
                Str = Str + "Nghieng chan khay i:" +"\t"+ txtNghiengMong .Text + "\r\n";
                Str = Str + "Sau mong gia co H2:" +"\t"+ TxtSauMongGc .Text + "\r\n";
                Str = Str + "Day chan khay b2:" +"\t"+ TxtDayMongGc .Text + "\r\n";
                Str = Str + "Day lop demlot H3:" +"\t"+ txtLopdem .Text + "\r\n";
                Str = Str + "Rong san gia co b:" +"\t"+ txtRongsanGc .Text + "\r\n";
                Str = Str + "Mo rong san b3:" +"\t"+ txtMorongsan .Text + "\r\n";
                Str = Str + "Gan theo rong san:" +"\t"+ opt_sanb.Checked.ToString() + "\r\n";
                Str = Str + "Gan theo mo rong:" +"\t"+ opt_sanb3.Checked.ToString() + "\r\n";
                Str = Str + "Rong dinh taluy b4:" +"\t"+ txtRongtaluyB1 .Text + "\r\n";
                Str = Str + "Rong dinh taluy b5:" +"\t"+ txtRongtaluyB2 .Text + "\r\n";
                Str = Str + "Bo tri chan khay quanh san gia co:" +"\t"+ txtBotriCk.Checked.ToString()+ "\r\n";
                Str = Str + "Rong b5=rong san cong:" + "\t" + txtChantaluy.Checked.ToString();
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }

           // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.dau)|*.dau";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Tuong dau
                txtTDau_cao .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_DayMTr .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_DayMSau .Text=Values[i].Split('\t')[1]; i++;
                txtCaotaluyh1 .Text=Values[i].Split('\t')[1]; i++;
                txtCaotaluyh2 .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_Day .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongT .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongS .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_Rongmong .Text=Values[i].Split('\t')[1]; i++;
                opt_b3.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                opt_b .Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                txtTDau_mrongb4 .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_doc .Text=Values[i].Split('\t')[1]; i++;
                //Tuong canh
                i = i + 1;
                TxtTcCaoH1 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcCaoH2 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcSauCk .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDaySan .Text=Values[i].Split('\t')[1]; i++;
                txtTcMrongtuong .Text=Values[i].Split('\t')[1]; i++;
                txtDuoiTc .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDayCk .Text=Values[i].Split('\t')[1]; i++;
                txtCkTrongTc .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDaytuong .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongB .Text=Values[i].Split('\t')[1]; i++;
                TxtTcMorongm2 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcGoca1 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcGoca2 .Text=Values[i].Split('\t')[1]; i++;
                txtTcDocm .Text=Values[i].Split('\t')[1]; i++;
                //Gia co
                i = i + 1;
                txtBotriSgc .Text=Values[i].Split('\t')[1]; i++;
                TxtDaySanGc .Text=Values[i].Split('\t')[1]; i++;
                TxtDaiSanGc .Text=Values[i].Split('\t')[1]; i++;
                txtNghiengMong .Text=Values[i].Split('\t')[1]; i++;
                TxtSauMongGc .Text=Values[i].Split('\t')[1]; i++;
                TxtDayMongGc .Text=Values[i].Split('\t')[1]; i++;
                txtLopdem .Text=Values[i].Split('\t')[1]; i++;
                txtRongsanGc .Text=Values[i].Split('\t')[1]; i++;
                txtMorongsan .Text=Values[i].Split('\t')[1]; i++;
                opt_sanb .Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                opt_sanb3.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                txtRongtaluyB1 .Text=Values[i].Split('\t')[1]; i++;
                txtRongtaluyB2 .Text=Values[i].Split('\t')[1]; i++;
                txtBotriCk.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                txtChantaluy.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmTdTc_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
